/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphicEngine;

import gameEngine.Ball;
import gameEngine.GameData;
import gameEngine.Player;
import gameEngine.Position;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JPanel;

/**
 *
 * @author Rémi Dubois <RemiEric.Dubois@gmail.com>
 */
public class GameDisplay extends JPanel implements Observer  {
    
    public static final int DEFAULT_WIDTH = 500;  /** Draw panel width */
    public static final int DEFAULT_HEIGHT = 500; /** Draw panel height */
    
    private Position ballPos;
    private Position blueTeamPos[];
    private Position redTeamPos[];
    private int teamSize;
    
    public GameDisplay(int fieldWidth, int fieldHeight, int teamSize) {
        this.setPreferredSize(new Dimension(fieldWidth, fieldHeight));
        ballPos = new Position();
        this.teamSize = teamSize;
        blueTeamPos = new Position[teamSize];
        redTeamPos = new Position[teamSize];
    }
    
    @Override
    public void paintComponent(Graphics g) {
        
        /* Draw the field */
        g.setColor(Color.GREEN);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        
        /* Draw the ball */
        g.setColor(Color.WHITE);
        g.fillOval(ballPos.getX()-(Ball.SIZE/2), ballPos.getY()-(Ball.SIZE/2), Ball.SIZE, Ball.SIZE);
        
        /* Draw the blue team */
        g.setColor(Color.BLUE);
        for(int i = 0; i < teamSize; i++) {
            g.fillOval(blueTeamPos[i].getX()-(Player.SIZE/2), blueTeamPos[i].getY()-(Player.SIZE/2), Player.SIZE, Player.SIZE);
        }
        
        /* Draw the red team */
        g.setColor(Color.RED);
        for(int i = 0; i < teamSize; i++) {
            g.fillOval(redTeamPos[i].getX()-(Player.SIZE/2), redTeamPos[i].getY()-(Player.SIZE/2), Player.SIZE, Player.SIZE);
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        if(o instanceof GameData) {
            GameData gameData = (GameData) o;
            
            ballPos = gameData.getBall().get_position();
            
            Player team1[] = gameData.getTeam1();
            for(int i = 0; i < teamSize; i++) {
                blueTeamPos[i] = team1[i].get_position();
            }
            
            Player team2[] = gameData.getTeam2();
            for(int i = 0; i < teamSize; i++) {
                redTeamPos[i] = team2[i].get_position();
            }
            
            this.repaint();
        }
    }
    
    //debug part
    public float dir = (float) 0.0;
    public float spe = (float) 0.0;
}
